package com.gxaedu.shiro.controller;

import com.gxaedu.shiro.dto.ResponseData;
import com.gxaedu.shiro.dto.ResultData;
import com.gxaedu.shiro.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/login")
public class LoginController {

    @GetMapping("/page")
    public String page(){
        return "login";
    }

    @PostMapping("/do")
    @ResponseBody
    public ResultData userLogin(User user, HttpServletRequest request){
        // 利用Shiro的登陆借口
        // 1. 获取主体
        Subject subject = SecurityUtils.getSubject();
        // 2. 准备token
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getUserPassword());
        // 3. 登陆
        subject.login(token);
        return ResponseData.success("登陆成功!","");
    }

}
